package com.campusevaluation.config;

import cn.hutool.core.collection.CollectionUtil;
import com.campusevaluation.interceptor.LoginInterceptor;
import com.campusevaluation.utils.JwtUtil;
import lombok.RequiredArgsConstructor;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import java.util.List;

@Configuration
@RequiredArgsConstructor
@EnableConfigurationProperties(AuthProperties.class)
public class MvcConfig implements WebMvcConfigurer {
    private final AuthProperties authProperties;
    private final JwtUtil jwtUtil;

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        // 添加拦截器
        LoginInterceptor loginInterceptor = new LoginInterceptor(jwtUtil);
        InterceptorRegistration interceptorRegistration = registry.addInterceptor(loginInterceptor);

        // 配置拦截路径
        List<String> includePath = authProperties.getIncludePaths();
        if (CollectionUtil.isNotEmpty(includePath)) {
            interceptorRegistration.addPathPatterns(includePath);
        }

        // 配置放行路径
        List<String> excludePath = authProperties.getExcludePaths();
        if (CollectionUtil.isNotEmpty(excludePath)) {
            interceptorRegistration.excludePathPatterns(excludePath);
        }
        interceptorRegistration.excludePathPatterns(
                "/error",
                "/favicon.ico",
                "/v2/**",
                "/v3/**",
                "/swagger-resources/**",
                "/swagger-ui/**",
                "/webjars/**",
                "/doc.html",
                "/login",
                "/register",
                "/code"
        );
    }
}
